﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using taigu.WeChat;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;


namespace taigu
{
    /*
     * 只实现：/common/getOrgStructList，目前这个方法里写的数据有问题，目前可以全部不用管
    * 文件名:OrganizationStructService
    * 功能：组织机构管理 
    * 创建时间：2021-11-15
    * 作者:gavin
    */
    [AllowAnonymous]
    public class OrganizationStructService : ApplicationService, IOrganizationStructService
    {
        private readonly IRepository<WeChat.OrganizationStruct, Guid> _repository;
        public OrganizationStructService(IRepository<WeChat.OrganizationStruct, Guid> repository, IConfiguration configuration)
        {
            this._configuration = configuration;
            this._repository = repository;
        }
        private IConfiguration _configuration;

        [Route("/common/getOrgStructList"),HttpPost]
        public Task<List<OrganizationStructDto>> GetListAsync(OrganizationStructSearchDto searchPostInfoDto)
        {


            return null;
        }
        
        /// <summary>
        /// 创建组织构造
        /// </summary>
        /// <param name="createPostInfoDto">创建DTO</param>
        /// <returns></returns>
        [AllowAnonymous]
        [Route("/common/createOrgStructList"),HttpPost]
        public async Task<ResData<string>> CreateAsync(OrganizationStructCreateDto createPostInfoDto)
        {
            try
            {
                OrganizationStruct data = ObjectMapper.Map<OrganizationStructCreateDto, OrganizationStruct>(createPostInfoDto);
                data.status = PostStatus.启用;
                var result = await _repository.InsertAsync(data);
                return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
            }
            catch
            {
                return new ResData<string>() { Status = ResStatus.failed, Msg = "fail", Info = "" };
            }
          
        }
        [AllowAnonymous]
        [Route("/common/updateOrgStructList"), HttpPost]
        public async Task<ResData<string>> UpdateAsync(OrganizationStructUpdateDto updateDto)
        {
            OrganizationStruct model = await _repository.GetAsync(x => x.Id == updateDto.id);
            if (model == null)
            {
                OrganizationStruct data = ObjectMapper.Map<OrganizationStructUpdateDto, OrganizationStruct>(updateDto);
                data.status = PostStatus.启用;
                var result = await _repository.InsertAsync(data);
                return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
            }
            else
            {
                model.status = PostStatus.删除;
                await _repository.UpdateAsync(model);
                return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
            }
        }

        /// <summary>
        /// 删除帖子
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [Route("/common/deleteOrgStructList"), HttpPost]
        public async Task<ResData<string>> DeleteAsync(Guid id)
        {
            try
            {
                await _repository.DeleteAsync(id);
                return new ResData<string>() { Status = ResStatus.成功, Msg = "sucess", Info = "" };
            }
            catch
            {
                return new ResData<string>() { Status = ResStatus.failed, Msg = "sucess", Info = "" };
            }

        }



        #region 新建
        /// <summary>
        /// 查询组织架构
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>

        [HttpGet, Route("OrganizationStructList")]
        public async Task<List<OrganizationStructListsDTO>> OrganizationStructList(Guid Id)
        {
            List<OrganizationStructListsDTO> organizationStructListDTOs = new List<OrganizationStructListsDTO>();
            var result = await _repository.GetListAsync();
            result = result.Where(x => x.parent_id == Id).ToList();
            foreach (var item in result)
            {
                organizationStructListDTOs.Add(new OrganizationStructListsDTO
                {
                    buId = item.Id,
                    buName = item.name,
                    child = await OrganizationStructList(item.Id)
                });
            }
            return organizationStructListDTOs;
        }
        /// <summary>
        /// 查询集合后返回的数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResData<List<OrganizationStructListsDTO>>> OutList(Guid Id)
        {
            try
            {
                var result = await OrganizationStructList(Id);
                if (result == null)
                {
                    return new ResData<List<OrganizationStructListsDTO>>
                    {
                        Status = ResStatus.查询失败,
                        Msg = "fail",
                        Info = null
                    };
                }
                else
                {
                    return new ResData<List<OrganizationStructListsDTO>>
                    {
                        Status = ResStatus.成功,
                        Msg = "success",
                        Info = await OrganizationStructList(Id)
                    };
                }
            }
            catch
            {
                return new ResData<List<OrganizationStructListsDTO>>
                {
                    Status = ResStatus.成功,
                    Msg = "success",
                    Info = await OrganizationStructList(Id)
                };
            }


        } 
        #endregion
    }
}
